module HTypes

implicit none

! File:     Types.f90
! Purpose:  This file contains declaration of derived types used in TASACS.
!           Normally one type is associated with a module.
!           Hence an object consists of a module with type members as properties and routines as behaviour.
! Contents: The following objects are declared in this file:
! History : When        Who        What
!           16-04-07    Skalevik   Created

! Observed data
integer, parameter :: odCaTon =            1 ! Catch in tonnes
integer, parameter :: odCaNum =            2 ! Catch in numbers 
integer, parameter :: odCaNumWeight =      3 ! Residual weighting factor of Catch in numbers
integer, parameter :: odWeCa =             4 ! Catch Weight in kg
integer, parameter :: odWeSt =             5 ! Stock Weight in kg
integer, parameter :: odMatProp =          6 ! Maturity ogive at spawning time
integer, parameter :: odFleet =            7 ! Fleet indices
integer, parameter :: odFleetWeight =      8 ! Residual weighting factor of Fleet indices
integer, parameter :: odSSB =              9 ! SSB indices
integer, parameter :: odSSBWeight =        10 ! Residual weighting factor of SSB indices
integer, parameter :: odBiomass =          11 ! Biomass indices
integer, parameter :: odBiomassWeight =    12 ! Residual weighting factor of Biomass indices
integer, parameter :: odFirst = odCaTon
integer, parameter :: odLast = odBiomassWeight

integer, parameter :: c_DataSource(0 : 4 - 1) = (/odCaNum, odFleet, odSSB, odBiomass/)

! Run out files 
integer, parameter :: roParamOut = 0
integer, parameter :: roSummary = 1
integer, parameter :: roOutput = 2
integer, parameter :: roBootResult = 3
integer, parameter :: roBootFMean = 4
integer, parameter :: roBootSSB = 5
integer, parameter :: roBootObj = 6
integer, parameter :: roRetroFMean = 7
integer, parameter :: roRetroSSB = 8
integer, parameter :: roRetroRecruit = 9
integer, parameter :: roFirst = roParamOut
integer, parameter :: roLast = roRetroRecruit


! Parameter flag
integer, parameter :: flKeepFixed = 0;
integer, parameter :: flEstimate =  1;
integer, parameter :: flEqualPrevYear = 2;
integer, parameter :: flSpecialCode = 3;
integer, parameter :: flEqualPrevAge = -1;

! Parameter categories
integer, parameter :: cNValues = 1;
integer, parameter :: cFYearFactor = 2;
integer, parameter :: cSelection = 3;
integer, parameter :: cNatMort = 4;
integer, parameter :: cCBL_Fac_Fleet = 5;
integer, parameter :: cCBL_Fac_SSB = 6;
integer, parameter :: cCBL_Fac_BM = 7;
integer, parameter :: cCBL_Pow_Fleet = 8;
integer, parameter :: cCBL_Pow_SSB = 9;
integer, parameter :: cCBL_Pow_BM = 10;
integer, parameter :: cGamma_Canum = 11;
integer, parameter :: cGamma_Fleet = 12;
integer, parameter :: cGamma_SSB = 13;
integer, parameter :: cGamma_BM = 14;
integer, parameter :: cFirstCategory = cNValues;
integer, parameter :: cLastCategory = cGamma_BM;


! File IO
integer, parameter :: cStockTitleLength = 80;
integer, parameter :: cTechNameLength = 10;
integer, parameter :: cFileNameLength = 256;

integer, parameter :: cFileUnit = 8;
!integer, parameter :: cPhiFile = 9;
integer, parameter :: cMessageFile = 10;
integer, parameter :: cTuningFile = 11;
!integer, parameter :: cObsModFile = 12;
integer, parameter :: cBootstrapFile = 13
integer, parameter :: cBootstrapResultFile = 14
integer, parameter :: cBootstrapFMeanFile = 15
integer, parameter :: cBootstrapSSBFile = 16
integer, parameter :: cBootstrapObjFuncFile = 17
integer, parameter :: cRetroFMeanFile = 18;
integer, parameter :: cRetroSSBFile = 19;
integer, parameter :: cRetroRecruitFile = 20;
integer, parameter :: cDsFile = 21
integer, parameter :: cIndexFile = 22;
integer, parameter :: cDataKeyFile = 23;


integer, parameter :: io_no_error = 0;

! Process type
integer, parameter :: ptOrdinaryAssessment = 0
integer, parameter :: ptSingleModelRun = 1
integer, parameter :: ptBootstrap = 2
integer, parameter :: ptRetroSpectiveRun = 3
integer, parameter :: ptParameterPertubation = 4


! Popuplation model type
integer, parameter :: pmtSep = 0
integer, parameter :: pmtVPA = 1
integer, parameter :: pmtISVPA = 2

! Plusgroup method
integer, parameter :: pgDirectFromCatch = 0
integer, parameter :: pgDynamicPool = 1

! ISVPA control type
integer, parameter :: ISVPA_CatchControl = 0
integer, parameter :: ISVPA_EffortControl = 1

!-- TObjType
integer, parameter :: otSSQ = 0
integer, parameter :: otMedian = 1
integer, parameter :: otAMD = 2
integer, parameter :: otGammaLikelihood = 3
integer, parameter :: otGammaDeviance = 4
integer, parameter :: otPoisson = 5


! Stock summary columns
integer, parameter :: ssRecruits = 0
integer, parameter :: ssTotalBiomass = 1
integer, parameter :: ssSSB = 2
integer, parameter :: ssFMean = 3
integer, parameter :: ssLandings = 4
integer, parameter :: ssFirst = ssRecruits
integer, parameter :: ssLast = ssLandings

integer, parameter :: c_ParamElmDescrLen = 50

! Run states
integer, parameter :: rsReset = 0
integer, parameter :: rsRunning = 1
integer, parameter :: rsRunningStep = 2
integer, parameter :: rsPause = 3
integer, parameter :: rsFinished = 4

! Run states
integer, parameter :: srGeneric = 0
integer, parameter :: srLBFGS = 1


!character(len = 256), parameter :: s_WorkingRun = "Working run"
character(len = 256), parameter :: s_DataFolder = "Data"
character(len = 256), parameter :: s_DataKeyFile = "DataKey.txt"
character(len = 256), parameter :: s_IndexFile = "index.txt"

character(len = 256), parameter :: s_LogFile = "log.txt"
!character(len = 256), parameter :: s_PhiFile = "phi.txt"
character(len = 256), parameter :: s_TuningFile = "tuning.txt"
!character(len = 256), parameter :: s_DsFile = "ds_summary.txt"
!character(len = 256), parameter :: s_ResultFile = "result.txt"
!character(len = 256), parameter :: s_BootstrapResultFile = "Bootstrap_Result.txt"
!character(len = 256), parameter :: s_BootstrapFMeanFile = "Bootstrap_FMean.txt"
!character(len = 256), parameter :: s_BootstrapSSBFile = "Bootstrap_SSB.txt"
!character(len = 256), parameter :: s_BootstrapObjFuncFile = "Bootstrap_ObjFunc.txt"
character(len = 10), parameter :: s_CurrentVersion = "VER 1.3"



double precision, parameter :: Fy_Smooth_Factor = 0.5
double precision, parameter :: S_Smooth_Factor = 0.5

! Run Properties:
! Index input properties
integer, parameter :: propProcessType = 0
integer, parameter :: propSearchRoutine = 1
integer, parameter :: propModelType = 2
integer, parameter :: propISVPAControlType = 3
integer, parameter :: propPlusGroupMethod = 4
integer, parameter :: propFirstYear = 5
integer, parameter :: propLastYear = 6
integer, parameter :: propLastCatchYear = 7
integer, parameter :: propFirstAge = 8
integer, parameter :: propLastAge = 9
integer, parameter :: propFirstRefAge = 10
integer, parameter :: propLastRefAge = 11
integer, parameter :: propNumMedianElms = 12
integer, parameter :: propGammaShape = 13
integer, parameter :: propNumFleetIndices = 14
integer, parameter :: propNumSSBIndices = 15
integer, parameter :: propNumBiomassIndices = 16
integer, parameter :: propNumStages = 17
integer, parameter :: propSSBFracF = 18
integer, parameter :: propSSBFracM = 19
integer, parameter :: propRunStage = 200
integer, parameter :: propRunIter = 201
integer, parameter :: propRunDelta = 202
integer, parameter :: propRunObjFunc = 203
integer, parameter :: propRunNumParam = 300
integer, parameter :: propProgramVersion = 301

integer, parameter :: propCaTonFile = 400
integer, parameter :: propCaNumFile = 401
integer, parameter :: propCaNumWeightFile = 402
integer, parameter :: propWeCaFile = 403
integer, parameter :: propWeStFile = 404
integer, parameter :: propMatPropFile = 405
integer, parameter :: propFleetFile = 406
integer, parameter :: propFleetWeightFile = 407
integer, parameter :: propSSBFile = 408
integer, parameter :: propSSBWeightFile = 409
integer, parameter :: propBiomassFile = 410
integer, parameter :: propBiomassWeightFile = 411
integer, parameter :: propParamIn = 412

integer, parameter :: propParamOutFile = 500
integer, parameter :: propSummaryFile = 501
integer, parameter :: propOutputFile = 502
integer, parameter :: propBootResultFile = 503
integer, parameter :: propBootFMeanFile = 504
integer, parameter :: propBootSSBFile = 505
integer, parameter :: propBootObjFile = 506
integer, parameter :: propRetroFMeanFile = 507
integer, parameter :: propRetroSSBFile = 508
integer, parameter :: propRetroRecruitFile = 509

integer, parameter :: propErrorStr = 600
integer, parameter :: propRunErrorStr = 601


!TBootstrapType
integer, parameter :: btOrgRes = 0
integer, parameter :: btOrgPar = 1
integer, parameter :: btModRes = 2
integer, parameter :: btModPar = 3
integer, parameter :: btExtData = 4


contains


function ModelTypeDescr(ModelType, ISVPAControlType)
   implicit none
   integer, intent(in) :: ModelType
   integer, intent(in) :: ISVPAControlType
   character(len = 20) :: ModelTypeDescr
   
   select case(ModelType)
      case(pmtSep)
         ModelTypeDescr = "Separable"
      case(pmtVPA)
         ModelTypeDescr = "VPA"
      case(pmtISVPA)
         select case(ISVPAControlType)
            case(ISVPA_CatchControl)
               ModelTypeDescr = "ISVPA - catch control"
            case(ISVPA_EffortControl)
               ModelTypeDescr = "ISVPA - effort control"
         end select
   end select
end function ModelTypeDescr


function ProcessTypeDescr(ProcessType)
   implicit none
   integer, intent(in) :: ProcessType
   character(len = 25) :: ProcessTypeDescr
   
   select case(ProcessType)
      case(ptOrdinaryAssessment)
         ProcessTypeDescr = "Ordinary assessment"
      case(ptSingleModelRun)
         ProcessTypeDescr = "Single model run"
      case(ptBootstrap)
         ProcessTypeDescr = "Bootstrap"
      case(ptRetroSpectiveRun)
         ProcessTypeDescr = "Retrospective run"
      case(ptParameterPertubation)
         ProcessTypeDescr = "Parameter pertubation"
   end select
end function ProcessTypeDescr


end module HTypes



